package com.solusiq.me.db;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * record interface for the database.
 * 
 * this interface designed to support serializable object attributes.
 * You can have non-native attribute in your "Recordable" objects.
 * But, these attributes should be an implementation of Recordable interface too.
 * 
 * Thats why we have serialize and dowrite(), also unserialize and doRead() methods.
 * 
 * For further understanding, please look at the examples. 
 * 
 * @author Amru Mubaidillah
 *
 */
public interface Recordable {

	/**
	 * 
	 * @return record id generated by RecordStore
	 */
	int getRecordId();
	
	/**
	 * used by Database class to set record number retrived from RecordStore
	 *  
	 * @return record id generated by RecordStore
	 */
	void setRecordId(int newId);
	
	/**
	 * Main entry when writing data to record store. 
	 * At the implementation stage, serialize should call doWrite if we have
	 * "Recordable" attributes in current object.
	 * 
	 * @see AbstractRecord
	 * 
	 * @return array of bytes which will be writen on Record Management Store (RMS)
	 */
	byte[] serialize();
	

	/**
	 * unserialize is main entry when we start
	 * 
	 * @param data array of bytes retrived from RMS
	 */
	void unserialize(byte[] data);
	
	/**
	 * write any attributes in this object which need to store.
	 * 
	 * <strong>important</strong>: the sequnce of attribute wrote to record store
	 * should be in equals sequence while reading.
	 * 
	 * @param outputStream
	 * @throws IOException
	 */
	void doWrite(DataOutputStream outputStream) throws IOException;
	
	/**
	 * retriving data from RecordStore and write to current object's attributes.
	 * 
	 * @param inputStream
	 * @throws IOException
	 */
	void doRead(DataInputStream inputStream) throws IOException;

}